package com.integrationpath.mengine.webapp.action;

import com.opensymphony.xwork2.Preparable;
import com.integrationpath.mengine.service.GenericManager;
import com.integrationpath.mengine.service.RoleManager;
import com.integrationpath.mengine.service.UtilityManager;
import com.integrationpath.mengine.model.Link;
import com.integrationpath.mengine.model.LinkCategory;
import com.integrationpath.mengine.model.LinkType;
import com.integrationpath.mengine.model.Role;
import com.integrationpath.mengine.webapp.action.BaseAction;

import java.util.ArrayList;
import java.util.List;

public class LinkAction extends BaseAction implements Preparable {
    private GenericManager<Link, Long> linkManager;
    private List links;
    private Link link;
    private Long  id;
	private List linkCategoryList;
	private List linkTypeList;	
	private List roles;
	private List linkOpenType;
	private GenericManager<LinkCategory, Integer> linkCategoryManager;
	private GenericManager<LinkType, Integer> linkTypeManager;
	private RoleManager roleManager;

	public RoleManager getRoleManager() {
		return roleManager;
	}

	public void setRoleManager(RoleManager roleManager) {
		this.roleManager = roleManager;
	}

	public List getRoles() {
		return roles;
	}

	public void setRoles(List roles) {
		this.roles = roles;
	}

	public List getLinkOpenType() {
		return linkOpenType;
	}

	public void setLinkOpenType(List linkOpenType) {
		this.linkOpenType = linkOpenType;
	}

	public List getLinkTypeList() {
		return linkTypeList;
	}

	public void setLinkTypeList(List linkTypeList) {
		this.linkTypeList = linkTypeList;
	}

	public GenericManager<LinkType, Integer> getLinkTypeManager() {
		return linkTypeManager;
	}

	public void setLinkTypeManager(GenericManager<LinkType, Integer> linkTypeManager) {
		this.linkTypeManager = linkTypeManager;
	}

	public GenericManager<LinkCategory, Integer> getLinkCategoryManager() {
		return linkCategoryManager;
	}

	public void setLinkCategoryManager(
			GenericManager<LinkCategory, Integer> linkCategoryManager) {
		this.linkCategoryManager = linkCategoryManager;
	}	
	public List getLinkCategoryList() {
		return linkCategoryList;
	}

	public void setLinkCategoryList(List linkCategoryList) {
		this.linkCategoryList = linkCategoryList;
	}
	
    public void setLinkManager(GenericManager<Link, Long> linkManager) {
        this.linkManager = linkManager;
    }

    public List getLinks() {
        return links;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
    	
		linkCategoryList = 	linkCategoryManager.getAll();
		linkTypeList = 	linkTypeManager.getAll();
//		roles = roleManager.getRoles(null);
		linkOpenType = populateOpenCase();
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String linkId = getRequest().getParameter("link.id");
            if (linkId != null && !linkId.equals("")) {
                link = linkManager.get(new Long(linkId));
                link.setLinkCategory(null);
               
            }
        }
    }

    public String list() {
        links = linkManager.getAll();
        return SUCCESS;
    }

    public void setId(Long  id) {
        this. id =  id;
    }

    public Link getLink() {
        return link;
    }

    public void setLink(Link link) {
        this.link = link;
    }

    public String delete() {
        linkManager.remove(link.getId());
        saveMessage(getText("link.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            link = linkManager.get(id);
        } else {
            link = new Link();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        LinkCategory linkCategory = linkCategoryManager.get(link.getLinkCategory().getId());
        LinkType linkType = linkTypeManager.get(link.getLinkCategory().getLinkType().getId());
//        linkType.setRole(roleManager.getRole(link.getLinkCategory().getLinkType().getRole().getName()));
        linkCategory.setLinkType(linkType);
		link.setLinkCategory(linkCategory);
					
        boolean isNew = (link.getId() == null);

        linkManager.save(link);

        String key = (isNew) ? "link.added" : "link.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
    
   private List populateOpenCase() {
	   List list = new ArrayList();
	   list.add(new OpenCase(0, "Blank"));
	   list.add(new OpenCase(1, "Self"));
	   return list;
   }
}

class OpenCase {
	Integer id;
	String name;
  
	public OpenCase(Integer id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}  
}
